package com.jml.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TcpServer {

	public static void main(String[] args) throws IOException {
		ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
		System.out.println("socket tcp服务器端启动....");
		 ServerSocket serverSocket = new ServerSocket(8080);
		final Socket accept = serverSocket.accept();
		// 等待客户端请求
		try {
			while (true) {
				//使用线程
				newCachedThreadPool.execute(new Runnable() {
					@Override
					public void run() {
						try {

							InputStream inputStream = accept.getInputStream();
							// 转换成string类型
							byte[] buf = new byte[1024];
							int len = inputStream.read(buf);
							String str = new String(buf, 0, len);
							System.out.println("服务器接受客户端内容:" + str);
							OutputStream outputStream = accept.getOutputStream();
							outputStream.write("qqqqqqqqqqqqqqq  server".getBytes());

						} catch (Exception e) {
						}
					}
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			serverSocket.close();
		}

	}

}
